package com.gl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gl.entity.EduCourse;
import com.gl.entity.EduTeacher;
import com.gl.service.EduCourseService;
import com.gl.service.EduTeacherService;
import com.gl.utils.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@RestController
@RequestMapping("/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //分页查询讲师
    @GetMapping("getPageList/{current}/{size}")
    public Map getPageList(@PathVariable long current,
                           @PathVariable long size){
        Map<String,Object> map = teacherService.getPageList(current,size);
        return map;
    }


    //根据讲师id查询讲师详情
    @GetMapping("getDetailById/{id}")
    public Map getDetailById(@PathVariable String id){
        Map<String,Object> map = teacherService.getDetailById(id);
        return map;
    }







    //后台
    //分页查询讲师列表
    @PostMapping("getBackTeacherList/{current}/{size}")
    public CommonResponse getBackTeacherList(@PathVariable long current,
                                             @PathVariable long size,
                                             @RequestBody(required = false) EduTeacher teacher){
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if(teacher!=null){
            //条件
            String name = teacher.getName();//讲师名称
            Integer level = teacher.getLevel();//讲师头衔
            //判断条件是否为空
            if(!StringUtils.isEmpty(name)){
                wrapper.like("name",name);
            }
            if(!StringUtils.isEmpty(level)){
                wrapper.eq("level",level);
            }
        }
        //按更新时间排序
        wrapper.orderByDesc("gmt_modified");

        Page<EduTeacher> page = new Page<>(current,size);
        teacherService.page(page,wrapper);
        List<EduTeacher> list = page.getRecords();//分页集合
        long total = page.getTotal();//总记录数
        return CommonResponse.ok().data("list",list).data("total",total);
    }

    //删除讲师
    @DeleteMapping("deleteById/{id}")
    public CommonResponse deleteById(@PathVariable String id){
        boolean isDel = teacherService.removeById(id);
        return CommonResponse.ok().data("del",isDel);
    }

    //添加讲师
    @PostMapping("saveTeacher")
    public CommonResponse saveTeacher(@RequestBody EduTeacher teacher){
        boolean add = teacherService.save(teacher);
        return CommonResponse.ok().data("add",add);
    }

    //根据id查询讲师
    @GetMapping("getBackById/{id}")
    public CommonResponse getBackById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return CommonResponse.ok().data("teacher",teacher);
    }

    //修改讲师
    @PostMapping("updateTeacher")
    public CommonResponse updateTeacher(@RequestBody EduTeacher teacher){
        boolean edit = teacherService.updateById(teacher);
        return CommonResponse.ok().data("edit",edit);
    }

    //查询所有讲师
    @GetMapping("getAllTeacher")
    public CommonResponse getAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return CommonResponse.ok().data("list",list);
    }


}

